package ${package.Controller};


import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>
import com.qianda.mall.common.validation.Create;
import com.qianda.mall.common.validation.Update;
import com.macro.mall.common.api.CommonResult;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import java.util.Optional;

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
@Api(tags = "${table.comment!}")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    @Autowired
    private ${table.serviceName} ${table.serviceName?uncap_first};

    @ApiOperation("新增${table.comment!}信息")
    @PostMapping
    public CommonResult post(@RequestBody @Validated(Create.class) ${entity} ${entity?uncap_first}){
        return ${table.serviceName?uncap_first}.save(${entity?uncap_first})?CommonResult.success(${entity?uncap_first}):CommonResult.failed();
    }

    @ApiOperation("修改${table.comment!}信息")
    @PutMapping
    public CommonResult put(@RequestBody @Validated(Update.class) ${entity} ${entity?uncap_first}){
        return ${table.serviceName?uncap_first}.updateById(${entity?uncap_first})?CommonResult.success(${entity?uncap_first}):CommonResult.failed();
    }

    @ApiOperation("获取${table.comment!}信息")
    @GetMapping("/{id}")
    public CommonResult get(@PathVariable @ApiParam(value = "主键") Long id){
        return Optional.ofNullable(${table.serviceName?uncap_first}.getById(id)).map(CommonResult::success).orElseGet(CommonResult::failed);
    }

    @ApiOperation("删除${table.comment!}信息")
    @DeleteMapping("/{id}")
    public CommonResult delete(@PathVariable @ApiParam(value = "主键") Long id){
        return ${table.serviceName?uncap_first}.removeById(id)?CommonResult.success(id):CommonResult.failed();
    }
}
</#if>
